package sis.ra.founded;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import sis.ra.utility.InxEntity;
import sis.ra.utility.Utils;


public class CandidateEntity {
	
			
	public static void writeToEntities()
	{
//		String filepathstr="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\testing company\\";//wiki//Amazon.com_wiki.txt";
//		String filepathstr="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo doc\\";
//		String filepathstr="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\cnet\\";
		String filepathstr="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\testing company\\";
		String outpath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\location\\";
		File filepath=new File (filepathstr);
		File[] files=filepath.listFiles();
		
		for (int i=0;i<files.length;i++)
		{
			if (!files[i].toString().contains(".segsent")) continue;
	//		System.out.println(files[i].toString());
			File thisfile=new File (files[i].toString().replace(".segsent", ".candidate").replace("testing company", "location"));
			if (thisfile.exists()) continue;
			String content=Utils.readFile(files[i].toString());
	//		System.out.println(content);
//			String companyname=files[i].toString().substring(66).replace(".segsent", "");
//			String filename=companyname;
			String filename=files[i].toString().replace(" C:\\Documents and Settings\\I820753\\Desktop\\dataset\\location\\", "").replace(".segsent", "");
//			//remove [1]
//			for (int b=1;b<200;b++)
//			content=content.replace("["+b+"]", "");
			
			HashMap<String, String> entityterm=new HashMap<String, String>();
			
			String[] lines=content.split("\n");
			for (int m=0;m<lines.length;m++)
			{
				if (lines[m].length()<1) continue;
				String sentid=lines[m].split("\t")[0];
				String sent=lines[m].split("\t")[1];
				String entityfile=Utils.getNE(sent);
	//		System.out.println(entityfile);
				ArrayList<InxEntity> entities=Utils.parseIMSXMLContentList(entityfile);
			
				for (int j=0;j<entities.size();j++)
				{
					if (entityterm.containsKey(entities.get(j).getCanoical()))
					{
						String appear=entityterm.get(entities.get(j).getCanoical());
						appear=appear+" "+filename.replace(" ", "_")+"-"+sentid+"-"+entities.get(j).getOffset();
						entityterm.remove(entities.get(j).getCanoical());
						entityterm.put(entities.get(j).getCanoical(), appear);
					}else
					{
						String appear=filename.replace(" ", "_")+"-"+sentid+"-"+entities.get(j).getOffset();
						entityterm.put(entities.get(j).getCanoical(), appear);
					}			
				}
			}
			System.out.println(files[i]+"\t"+entityterm.size());
			Iterator<String> its=entityterm.keySet().iterator();
			while (its.hasNext())
			{
				String term=its.next();
				System.out.println(term+"\t"+entityterm.get(term));
				Utils.writeToFile(files[i].toString().replace(".segsent", ".candidate").replace("testing company", "location"), term+"\t"+entityterm.get(term), true, true);
			}
	}
	}
		
	public static String readSentence(String sentenceid)
	{
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\testing company\\";
		String thissent="";
		
		String file=sentenceid.split("-")[0].replace("_", " ")+"_wiki.segsent";
		String content=Utils.readFile(filepath+file);
//		System.out.println(content);
		if (!sentenceid.contains("-")) return null;
		String sentenceno=sentenceid.split("-")[1];
		String [] lines=content.split("\n");
		boolean find=false;
		for (int i=0;i<lines.length;i++)
		{
			if (lines[i].startsWith(sentenceno))
			{
	//			System.out.println("==>"+lines[i]);
				thissent=lines[i];
				find=true;
			}
		}
		if (!find)
		{
			System.out.println(sentenceid);
			System.out.println("-->fail"+sentenceno);
		}
		return thissent;
	}
	public static void main(String[] args) {
		
		writeToEntities();
		
		
	}
}